package com.ruoyi.project.business.domain.rules.month;

import com.ruoyi.project.business.askleave.entity.AskLeave;
import com.ruoyi.project.business.askleave.service.IAskLeaveService;
import com.ruoyi.project.business.domain.entity.Emp;
import com.ruoyi.project.business.domain.exceptions.RuleException;
import com.ruoyi.project.business.domain.model.JudgeResult;
import com.ruoyi.project.business.domain.model.Solution;
import com.ruoyi.project.business.domain.rules.Rule;
import io.swagger.models.auth.In;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class AskLeaveDaysRule extends Rule {
    @Setter
    private IAskLeaveService askLeaveService;
    @Setter
    private List<Integer> aimDayOfMonths;
    @Setter
    private Long userId;
    @Setter
    private Long tenantId;
    @Setter
    private Integer year;
    @Setter
    private Integer month;

    @Override
    public JudgeResult judge(Solution solution, Emp emp) {
        List<AskLeave> leaves = askLeaveService.findUserLeaves(aimDayOfMonths, year, month, userId, tenantId);
        if(CollectionUtils.isNotEmpty(leaves)) {
            List<Integer> dayOfMonth = leaves.stream().map(AskLeave::getDayOfMonth).collect(Collectors.toList());
            throw new RuleException(year + "年" + month + "月" + StringUtils.join(dayOfMonth, ",") + "有请假, 不能排班");
        }
        return accept();
    }

    @Override
    public void setConflicts() {
        setType(Rule.RULE_TYPE_USER);
        this.conflicts = new ArrayList<>();
    }
}
